package com.yz.food.cms.controller;

import com.github.pagehelper.PageInfo;
import com.yz.food.cms.common.ServerResponse;
import com.yz.food.cms.pojo.Category;
import com.yz.food.cms.pojo.Food;
import com.yz.food.cms.service.CategoryService;
import com.yz.food.cms.service.FoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class FoodController {
    @Autowired
    private FoodService foodService;
    @Autowired
    private CategoryService categoryService;
    /**
     * 查询所有的食品
     */
    @RequestMapping("page/food")
    public String getAll(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                         @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                         ModelMap modelMap) {
        PageInfo<Food> pageInfo = foodService.getAll(pageNum, pageSize);
        modelMap.addAttribute("pageInfo", pageInfo);
        List<Category> categories = categoryService.getAll();
        modelMap.addAttribute("categories", categories);
        return "food";
    }

    /**
     * 更新食材
     */
    @PostMapping("food/updateFood")
    @ResponseBody
    public ServerResponse updateFood(Food food) {
        int result = foodService.updateFood(food);
        if (result > 0) {
            return ServerResponse.createBySuccessMessage("添加成功");
        }
        return ServerResponse.createByErrorMessage("添加失败");
    }
}
